import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;

public class javaCmd {

    public static void main(String[] args) throws IOException {
        switch (args[0]) {
            case "ls":
                switch (args.length) {
                    case 1:
                        ls();
                        break;
                    case 2:
                        File file = new File(args[1]);
                        if ("-l".equals(args[1])) {
                            lsLong();
                        } else if (file.exists() && file.isDirectory())
                            lsPath(args[1]);
                        else
                            System.out.println("输入不合法！");
                        break;
                    case 3:
                        File file1 = new File(args[1]);
                        if ("-l".equals(args[2]) && file1.exists() && file1.isDirectory())
                            lsPathLong(args[1]);
                        else
                            System.out.println("输入不合法！");
                        break;
                    default:
                        System.out.println("输入不合法！");
                        break;
                }
                break;
            case "cp":
                if (args.length == 3) {
                    File file = new File(args[1]);
                    if (file.exists() && file.isFile()) {
                        cp(args[2], file);
                    } else {
                        System.out.println("输入不合法！");
                    }
                } else {
                    System.out.println("输入不合法！");
                }
                break;
            case "rm":
                if (args.length == 2) {
                    File file = new File(args[1]);
                    if (file.exists()) {
                        delete(file);
                    }
                } else
                    System.out.println("输入不合法！");
                break;
            default:
                System.out.println("输入不合法！");
                break;
        }
    }


    // ls 后面不加路径
    public static void ls() {

        String currentPath = System.getProperty("user.dir");    // 获取当前路径
        File file = new File(currentPath);
        for (File listFile : file.listFiles()) {
            System.out.println(listFile.getName());
        }
    }

    // ls 后面加路径
    public static void lsPath(String path) {
        String currentPath = System.getProperty("user.dir");
        File file = new File(currentPath + "\\" + path);
        for (File listFile : file.listFiles()) {
            System.out.println(listFile.getName());
        }
    }

    // ls 后面加-l
    public static void lsLong() {
        String currentPath = System.getProperty("user.dir");
        File file = new File(currentPath);
        for (File listFile : file.listFiles()) {
            char dir = listFile.isDirectory() ? 'd' : '-';

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(listFile.lastModified());

            System.out.printf("%c   %d   %s   %s\n",dir,listFile.length(),format,listFile.getName());
        }
    }

    // ls 后面加路径和-l
    public static void lsPathLong(String path) {
        String currentPath = System.getProperty("user.dir");
        File file = new File(currentPath + "\\" + path);
        for (File listFile : file.listFiles()) {
            char dir = listFile.isDirectory() ? 'd' : '-';

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(listFile.lastModified());

            System.out.printf("%c   %d   %s   %s\n",dir,listFile.length(),format,listFile.getName());
        }

    }

    // 复制文件
    private static void cp(String arg, File file) throws IOException {
        File newFile = new File(arg);
        if (newFile.exists()) {
            newFile.delete();
            Files.copy(file.toPath(), newFile.toPath());
        }
        Files.copy(file.toPath(), newFile.toPath());
    }

    // 删除文件
    public static void delete(File file) {
        if (file.isDirectory()) {
            for (File f : file.listFiles()) {
                delete(f);
            }
        }
        file.delete();
    }
}
